package com.wejoy.video.middle.station.data.service.res.impl;

import com.wejoy.video.middle.station.data.domain.res.VideoResLib;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.enums.dict.EnumVideoResLibSourceType;
import com.wejoy.video.middle.station.data.enums.dict.MultiMediaUploaderType;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.mapper.res.IVideoResLibMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.res.IVideoResLibService;
import com.wejoy.video.middle.station.data.constant.RedisKeys;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.res.VideoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

/**
 * 视频资源库
 *
 * @author Administrator
 * @date 2021年10月25日 17:52:36
 */
@Service
public class VideoResLibServiceImpl implements IVideoResLibService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VideoResLibServiceImpl.class);

    @Autowired
    private IVideoResLibMapper videoResLibMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Value("${qiniu.prefix}")
    private String videoPrefix;

    @Override
    public void save(VideoResLib req, String operator) {
        if (null == req.getId()) {
            req.setId(idWorkerService.getNextId());
            req.setDataStatus(EnumStatus.AUDIT.getCode());
            req.setCreateTime(new Date());
            req.setCreateUserId(operator);
            //暂时默认上传者为内部上传者类型
            req.setUploaderType(MultiMediaUploaderType.INNER_UPLOADER.getCode());
            insertSelective(req);
        } else {
            VideoResLib queryCondition = new VideoResLib();
            queryCondition.setId(req.getId());
            VideoResLib dbVideoResLib = videoResLibMapper.getOne(queryCondition);
            if (dbVideoResLib == null) {
                throw new BusinessException("未找到该视频库资源");
            }
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(VideoResLib req) {
        checkStringLength(req, false);
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        return videoResLibMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(VideoResLib videoResLib) {
        WJAssert.notNull(videoResLib, "入参对象不能为空");
        WJAssert.notNull(videoResLib.getId(), "id参数错误");
        VideoResLib where = new VideoResLib();
        where.setId(videoResLib.getId());
        return videoResLibMapper.updateSelective(videoResLib, where);
    }

    @Override
    public int getCount(VideoResLib videoResLib) {
        checkStringLength(videoResLib, true);
        return videoResLibMapper.getCount(videoResLib);
    }

    @Override
    public VideoResLib getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        VideoResLib where = new VideoResLib();
        where.setId(id);
        return videoResLibMapper.getOne(where);
    }

    @Override
    public List<VideoResLib> getList(VideoResLib videoResLib) {
        checkStringLength(videoResLib, true);
        return videoResLibMapper.getList(videoResLib);
    }

    @Override
    public List<VideoResLib> getPageList(VideoResLib videoResLib) {
        checkStringLength(videoResLib, true);
        return videoResLibMapper.getPageList(videoResLib);
    }

    @Override
    public void audit(Long id, String operator) {
        VideoResLib req = new VideoResLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        VideoResLib req = new VideoResLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        VideoResLib req = new VideoResLib();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public VideoResLib selectOneByPath(String path) {
        WJAssert.notNull(path, "path参数错误");
        VideoResLib where = new VideoResLib();
        where.setPath(path);
        return videoResLibMapper.getOne(where);
    }

    @Override
    @Cacheable(value = RedisKeys.VIDEO_RES, key = "#path", condition = "#path != null", unless = "#result == null")
    public VideoVO getOneByPath(String path) {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        VideoResLib videoResLib = this.selectOneByPath(path);
        if (videoResLib == null) {
            LOGGER.error("视频路径错误，路径：{}", path);
            return null;
        }
        VideoVO videoVO = new VideoVO();
        videoVO.setPath(videoResLib.getPath());
        videoVO.setDuration(videoResLib.getDuration());
        videoVO.setSize(videoResLib.getFileSize());
        return videoVO;
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(VideoResLib videoResLib, boolean nullAble) {
        WJAssert.notNull(videoResLib, "入参对象不能为空");
        WJAssert.limitMaxLength(videoResLib.getExtensionName(), nullAble, 10, "扩展名不合法!");
        WJAssert.limitMaxLength(videoResLib.getPath(), nullAble, 255, "视频路径不合法!");
        WJAssert.limitMaxLength(videoResLib.getCreateUserId(), nullAble, 32, "上传用户id不合法!");
        WJAssert.limitMaxLength(videoResLib.getFileName(), nullAble, 255, "文件名称不合法!");
        WJAssert.limitMaxLength(videoResLib.getPoster(), true, 255, "海报不合法!");
        WJAssert.limitMaxLength(videoResLib.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(videoResLib.getRemark(), true, 255, "备注不合法!");
    }
}
